<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.util.*" %>
<%@ page import="java.io.*" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="jxl.*" %>
<%@ page import="jxl.write.*" %>
<% 
	request.setCharacterEncoding("UTF-8");
	String stuId = request.getParameter("stuId").trim();
	String stuName = request.getParameter("stuName").trim();;
	String birth = request.getParameter("birth").trim();
	Calendar cal = Calendar.getInstance();
	cal.set(Integer.parseInt(birth.split("-")[0]),Integer.parseInt(birth.split("-")[1])-1,Integer.parseInt(birth.split("-")[2]));
	Date date = cal.getTime();
	SimpleDateFormat datef = new SimpleDateFormat("yyyy年MM月dd日");
	birth = datef.format(date).toString();
	File fileName = new File("D:/Java/temp.xls");
	try{
		if(!fileName.exists()){
			WritableWorkbook workbook  =  Workbook.createWorkbook (fileName);
			WritableSheet sheettemp = workbook.createSheet("sheet1", 0);
			Label label = new Label(0,0,"学号");
			sheettemp.addCell(label);
			label = new Label(1,0,"姓名");
			sheettemp.addCell(label);
			label = new Label(2,0,"出生日期");
			sheettemp.addCell(label);
			workbook.write();
			workbook.close();
		}
		Workbook book = Workbook.getWorkbook(fileName);
		Sheet sheet = book.getSheet(0); 
		int len = sheet.getRows();
		String tempstr = null;
		for(int i=0;i<len;i++){
			tempstr = sheet.getCell(0, i).getContents();
			if(stuId.equals(tempstr)){
				out.print("<a href='instuinfo.jsp'>该学生的信息已经存在，请返回重新输入！</a>");
				return ;
			}
		}
		WritableWorkbook workbook  =  Workbook.createWorkbook (fileName,book);
		WritableSheet sheet1 = workbook.getSheet(0); 
		Label label1 = new Label(0,len,stuId);
		sheet1.addCell(label1);
		label1 = new Label(1,len,stuName);
		sheet1.addCell(label1);
		label1 = new Label(2,len,birth);
		sheet1.addCell(label1);
		workbook.write(); 
		out.print("<script>alert('学生信息已经成功输入，请返回！');</script>");
		out.print("<script>window.location='instuinfo.jsp';</script>");
		workbook.close();			
		book.close(); 
	}catch(Exception e){
		out.print(e.getMessage());
	}
/* 	File fileName = new File("D:/Java/stuinfo.txt");
	try{				
		if(!fileName.exists()){
			FileWriter fw = new FileWriter(fileName);
			fileName.createNewFile();
			fw.write("学号\t姓名\t出生日期");
			fw.flush();
			fw.close();
		}
		FileReader fr = new FileReader(fileName);
		BufferedReader br = new BufferedReader(fr);
		String str = null;
		int flag = 0;
		while((str=br.readLine())!=null){
			if(str!=null){
				str = str.split("\\t")[0];
				if(stuId.equals(str)){
					out.print("<a href='instuinfo.jsp'>该学生的信息已经存在，请返回重新输入！</a>");
					return ;  
					//out.close();
				}else{
					flag = 1;
				}
			}
		}
		 if(flag==1){
			FileWriter fw = new FileWriter(fileName,true);
			fw.write("\r\n"+stuId+"\t"+stuName+"\t"+birth);
			out.print("<a href='instuinfo.jsp'>学生信息已经成功输入，请返回</a>");
			fw.flush();
			fw.close();			
		}
		br.close();
		fr.close();

	}catch(Exception e){
		out.print(e.getMessage());
	} */
	

%>
